package client.domain;

/**
 * User: nbarrios
 * Date: Nov 11, 2009
 */
public class SandboxUtil {

	public static final int URL_PARAMETERS_SEPARATOR = '?';
	public static final String PARAMETERS_SEPARATOR = "&";
	public static final String PARAMETER_ASIGNATOR = "=";

	/**
	 * Add a parameter with its value assuming that if the URL has not got any parameter then the last character is a "?"
	 *
	 * @param url
	 * @param parameterName
	 * @param parameterValue
	 * @return The URL with the new couple of parameter name and value or null if any of the arguments is null.
	 */
	public static String addParameterToURL(String url, String parameterName, String parameterValue){
		if(url!=null && parameterName!=null && parameterValue!=null){
			StringBuffer stringBuffer = new StringBuffer(url);

			if(stringBuffer.charAt(stringBuffer.length()-1)==URL_PARAMETERS_SEPARATOR)
				stringBuffer.append(parameterName).append(PARAMETER_ASIGNATOR).append(parameterValue);
			else
				stringBuffer.append(PARAMETERS_SEPARATOR).append(parameterName).append(PARAMETER_ASIGNATOR).append(parameterValue);

			return deleteWhiteSpaces(stringBuffer.toString());
		}
		else
			return null;
	}


	/**
	 * Borra todos los espacios en blanco de un String que recibe como parametro
	 * @param st del cual quiero borrar todos los espacios en blanco que haya.
	 * @return String sin espacios en blanco
	 */
	public static String deleteWhiteSpaces(String st){
		if(st==null || st.equals(""))
			return null;

		int whiteSpace = st.indexOf(' ');
		while(whiteSpace>=0){
			st = st.substring(0,whiteSpace).concat(st.substring(whiteSpace+1));
			whiteSpace = st.indexOf(' ');
		}

		return (st.equals(""))?null:st;
	}


}//  End Class
